#include "TianwangFile.hpp"
#include "util/CheckEncoding.hpp"
#include "util/String.hpp"
// #include "util/Unicode.hpp"
// #include "util/Conv.hpp"
#include "util/Convert.hpp"

#include <iostream>
#include <fstream>

const int WINDOW_SIZE = 20;

using namespace std;

struct elem
{
    string url;
    string content;
};

string
RemoveHtmlTag(const string& rWebPage)
{
    string result;
    string::size_type pos_l, pos_r;
    pos_l = pos_r = 0;
    do {
        pos_r = rWebPage.find(">", pos_l);
        pos_l = rWebPage.find("<", pos_r);
        if (pos_l != string::npos && pos_l > pos_r) {
            if (pos_l > pos_r+1)
                result += rWebPage.substr(pos_r + 1, pos_l - pos_r - 1);
        }
    } while(pos_l != string::npos && pos_r != string::npos);
    return result;
}

int
PrintResult(const vector<elem>& crElemVec)
{
    for (unsigned i=0; i<crElemVec.size(); ++i) {
        cout << crElemVec[i].url << endl
             << crElemVec[i].content << endl
             << endl;
    }
    return 0;
}


int
main(int argc, char** argv)
{
    if (argc < 2) {
        cerr << "Input search word!" << endl;
        exit(-1);
    }
    string path_tw;
    path_tw = "./result/";
    //     cin >> path_tw;
    if (path_tw[path_tw.length()-1] != '/') {
        path_tw += "/";
    }

    string word;
    //     cin >> word;
    word = argv[1];

    TianwangFile tw_file;
    Page page;

    ifstream infile;
    int tw_idx = 0;
    string cur_file = path_tw + String::Type2String<int>(tw_idx) + ".tw";

    //     CUnicode unicode;
    //     CConv conv("GB18030", "UTF8");
    Convert con_gb("GB2312");
    Convert con_utf("UTF8");
    wstring word_w;
    if (!con_utf.ToUnicode(word_w, word)) {
        cerr << "Invalid input: " << word << endl;
    }
    
    //     wstring word_w = unicode.mbs_to_wcs(word);
    vector<elem> elem_vec;
    infile.open(cur_file.c_str());

    CheckEncoding check;
    
    while (infile) {
        while (tw_file.Read(infile, page) != -1) {
            string web_page = page.GetContent();
            string encoding = check.GetEncoding(web_page).substr(0, 2);
            web_page = RemoveHtmlTag(web_page);
            wstring web_page_w;
            if (encoding == "gb" || encoding == "GB") {
                cout << "gb code!" << endl;
                if (con_gb.ToUnicode(web_page_w, web_page)) {
                    cerr << "error in convert code in gb format" << endl;
                    cerr << web_page_w.length() << endl;
                }
                //                 web_page = conv.conv(web_page);
            } else {
                if (con_utf.ToUnicode(web_page_w, web_page)) {
                    cerr << "error in convert code in utf format" << endl;
                    cerr << web_page_w.length() << endl;
                }
                //                 wstring web_page_w = unicode.mbs_to_wcs(web_page);
            }
            wstring::size_type pos = web_page_w.find(word_w);
            if (pos != wstring::npos) {
                elem e;
                e.url = page.GetOrginUrl();
                wstring::size_type pos_2;
                if (pos > 10) {
                    pos_2 = pos - 10;
                } else {
                    pos_2 = 0;
                }
                wstring window = web_page_w.substr(pos_2, WINDOW_SIZE);
                //                 e.content = unicode.wcs_to_mbs(window);
                con_utf.ToMultibyte(e.content, window);
                String::EraseStr(e.content, " ");
                String::EraseStr(e.content, "\n");
                String::EraseStr(e.content, "\t");
                elem_vec.push_back(e);
            }
        }
        infile.close();

        ++tw_idx;
        cur_file = path_tw + String::Type2String<int>(tw_idx) + ".tw";
        infile.open(cur_file.c_str());
    }
    PrintResult(elem_vec);
}

